package com.ctlele.team.space.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ctlele.team.space.R;
import com.ctlele.team.space.activity.BaseActivity;
import com.ctlele.team.space.entity.vo.ChatMessageVo;
import com.ctlele.team.space.util.ImageUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class ChatMsgItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final String TAG = "ChatMsgItemAdapter";

    private String mMe;

    private String mFriend;

    private BaseActivity mActivity;

    private List<ChatMessageVo> mMessageList = new ArrayList<>();

    private RecyclerView mParent;

    private static final int FRIEND_MESSAGE = 20011003;

    private static final int MY_MESSAGE = 20010820;

    public ChatMsgItemAdapter(BaseActivity activity, RecyclerView parent) {
        mActivity = activity;
        mParent = parent;
    }

    public void setMe(String me) {
        mMe = me;
    }

    public void setFriend(String friend) {
        mFriend = friend;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == FRIEND_MESSAGE) {
            // 使用好友布局
            view = LayoutInflater.from(mActivity).inflate(R.layout.item_chat_msg_other_layout, parent, false);
        } else {
            view = LayoutInflater.from(mActivity).inflate(R.layout.item_chat_msg_my_layout, parent, false);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ChatMessageVo message = mMessageList.get(position);

        ViewHolder vh = (ViewHolder) holder;
        vh.msg.setText(message.getMsg());
        Picasso.with(mActivity).load(message.getFromAvatar()).transform(ImageUtils.getCircleImageTransformation()).into(vh.avatar);
    }

    @Override
    public int getItemCount() {
        return mMessageList == null ? 0 : mMessageList.size();
    }

    @Override
    public int getItemViewType(int position) {
        ChatMessageVo vo = mMessageList.get(position);
        if (vo.getFrom().equals(mFriend)) {
            // 来自于好友的消息
            return FRIEND_MESSAGE;
        }
        return MY_MESSAGE;
    }

    public void flushData(List<ChatMessageVo> messageList) {
        mMessageList = messageList;
        // mMessageList.sort((o1, o2) -> -Long.compare(o1.getGmtCreate().getTime(), o2.getGmtCreate().getTime()));
        mActivity.runOnUiThread(() -> {
            notifyDataSetChanged();
            scrollToBottom();
        });
    }

    public void appendData(List<ChatMessageVo> messageList) {
        if (messageList == null || messageList.isEmpty()) {
            return;
        }
        if (mMessageList == null || mMessageList.isEmpty()) {
            flushData(messageList);
            return;
        }

        int size = mMessageList.size();
        mMessageList.addAll(messageList);
        // 按时间倒序排序
        // mMessageList.sort((o1, o2) -> -Long.compare(o1.getGmtCreate().getTime(), o2.getGmtCreate().getTime()));
        mActivity.runOnUiThread(() -> {
            notifyItemRangeChanged(size, mMessageList.size());
            scrollToBottom();
        });
    }

    public void appendData(ChatMessageVo message) {
        if (message == null) {
            return;
        }
        if (mMessageList == null || mMessageList.isEmpty()) {
            List<ChatMessageVo> tmp = new ArrayList<>();
            tmp.add(message);
            flushData(tmp);
            return;
        }

        int size = mMessageList.size();
        mMessageList.add(message);
        // 按时间倒序排序
        // mMessageList.sort((o1, o2) -> -Long.compare(o1.getGmtCreate().getTime(), o2.getGmtCreate().getTime()));
        mActivity.runOnUiThread(() -> {
            notifyItemRangeChanged(size, mMessageList.size());
            scrollToBottom();
        });
    }

    void scrollToBottom() {
        int itemCount = getItemCount();
        mParent.smoothScrollToPosition(itemCount - 1);
    }

    public List<ChatMessageVo> getMessageList() {
        return mMessageList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView msg;

        private final ImageView avatar;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            msg = itemView.findViewById(R.id.msg_content);
            avatar = itemView.findViewById(R.id.avatar);

            itemView.setTag(this);
        }
    }
}
